﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Services;

namespace MvcApp.Models
{
    public class TreeDataOfChildren
    {
        public Guid id { get; set; }
        public string text { get; set; }
        public List<TreeDataOfChildren> children { get; set; }

        public List<TreeDataOfChildren> Get(IOrganizeService organizeService, Guid id)
        {
            List<TreeDataOfChildren> tds = new List<TreeDataOfChildren>();
            ISpecification<Organize> condition = new DirectSpecification<Organize>(o => o.ParentId == id);
            Expression<Func<Organize,string>> orderbyExpression = o => o.Code;
            List<Organize> organizes = organizeService.GetList<string>(condition, orderbyExpression, false);            
            if (organizes.Count > 0)
            {
                foreach(var org in organizes)
                {
                    TreeDataOfChildren td = new TreeDataOfChildren();
                    td.id = org.Id;
                    td.text = org.OrganizeName;
                    td.children = this.Get(organizeService, org.Id);
                    tds.Add(td);
                }
            }
            return tds;
        }
     }
}